from sklearn import datasets
from sklearn.ensemble import AdaBoostRegressor
from sklearn.metrics import mean_squared_error, explained_variance_score
from sklearn.tree import DecisionTreeRegressor
from sklearn.utils import shuffle
import pandas as pd
import numpy as np

def load_boston():
    data_url = "http://lib.stat.cmu.edu/datasets/boston"
    raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
    data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
    target = raw_df.values[1::2, 2]
    return data, target


# housing_data = datasets.load_boston()
# x, y = shuffle(housing_data.data, housing_data.target, random_state=7)
data, target = load_boston()
x, y = shuffle(data, target, random_state=7)
num_training = int(0.8 * len(x))
x_train = x[:num_training]
y_train = y[:num_training]
x_test = x[num_training:]
y_test = y[num_training:]
dt_regressor = DecisionTreeRegressor(max_depth=4)
dt_regressor.fit(x_train, y_train)
ab_regressor = AdaBoostRegressor(DecisionTreeRegressor(max_depth=4), n_estimators=400, random_state=7)
ab_regressor.fit(x_train, y_train)

y_pred_dt = dt_regressor.predict(x_test)
mse = mean_squared_error(y_test, y_pred_dt)
evs=explained_variance_score(y_test, y_pred_dt)
print(f'decision tree mse: {round(mse,2)},evs:{round(evs,2)}')

y_pred_ab = ab_regressor.predict(x_test)
mse = mean_squared_error(y_test, y_pred_ab)
evs=explained_variance_score(y_test, y_pred_ab)
print(f'adaboost mse: {round(mse,2)},evs:{round(evs,2)}')